package cn.iocoder.yudao.module.erp.controller.admin.product;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.erp.controller.admin.product.vo.product.ErpProductPageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.product.vo.product.ErpProductRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.product.vo.product.ErpProductStockWeightRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.product.vo.product.ProductSaveReqVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.product.ErpProductDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.stock.ErpStockDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.stock.ErpWarehouseDO;
import cn.iocoder.yudao.module.erp.service.product.ErpProductService;
import cn.iocoder.yudao.module.erp.service.stock.ErpStockService;
import cn.iocoder.yudao.module.erp.service.stock.ErpWarehouseService;
import com.mzt.logapi.starter.annotation.LogRecord;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;

@Tag(name = "管理后台 - ERP 产品")
@RestController
@RequestMapping("/erp/product")
@Validated
public class ErpProductController {

    @Resource
    private ErpProductService productService;

    @Resource
    private ErpStockService stockService;

    @Resource
    private ErpWarehouseService warehouseService;

    @PostMapping("/create")
    @Operation(summary = "创建产品")
    @PreAuthorize("@ss.hasPermission('erp:product:create')")
    @LogRecord(type = "产品管理", subType = "创建产品", bizNo = "{{#result.id}}", success = "创建产品成功")
    public CommonResult<Long> createProduct(@Valid @RequestBody ProductSaveReqVO createReqVO) {
        return success(productService.createProduct(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新产品")
    @PreAuthorize("@ss.hasPermission('erp:product:update')")
    @LogRecord(type = "产品管理", subType = "更新产品", bizNo = "{{#updateReqVO.id}}", success = "更新产品成功")
    public CommonResult<Boolean> updateProduct(@Valid @RequestBody ProductSaveReqVO updateReqVO) {
        productService.updateProduct(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除产品")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:product:delete')")
    @LogRecord(type = "产品管理", subType = "删除产品", bizNo = "{{#id}}", success = "删除产品成功")
    public CommonResult<Boolean> deleteProduct(@RequestParam("id") Long id) {
        productService.deleteProduct(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得产品")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:product:query')")
    public CommonResult<ErpProductRespVO> getProduct(@RequestParam("id") Long id) {
        ErpProductDO product = productService.getProduct(id);
        return success(BeanUtils.toBean(product, ErpProductRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得产品分页")
    @PreAuthorize("@ss.hasPermission('erp:product:query')")
    public CommonResult<PageResult<ErpProductRespVO>> getProductPage(@Valid ErpProductPageReqVO pageReqVO) {
        return success(productService.getProductVOPage(pageReqVO));
    }

    @GetMapping("/simple-list")
    @Operation(summary = "获得产品精简列表", description = "只包含被开启的产品，主要用于前端的下拉选项")
    public CommonResult<List<ErpProductRespVO>> getProductSimpleList() {
        List<ErpProductRespVO> list = productService.getProductVOListByStatus(CommonStatusEnum.ENABLE.getStatus());
        return success(convertList(list, product -> new ErpProductRespVO().setId(product.getId())
                .setName(product.getName())
                .setBarCode(product.getBarCode())
                //.setCategoryId(product.getCategoryId())
                .setCategoryName(product.getCategoryName())
                //.setUnitId(product.getUnitId())
                .setUnitName(product.getUnitName())
//                .setPurchasePrice(product.getPurchasePrice())
//                .setSalePrice(product.getSalePrice())
                .setStock(product.getStock())
                .setWeight(product.getWeight())
                .setGoldFinenessName(product.getGoldFinenessName())
                .setMinPrice(product.getMinPrice())));
    }

    @GetMapping("/stock-weight")
    @Operation(summary = "查询商品库存和克重信息")
    @Parameters({
            @Parameter(name = "id", description = "商品编号", required = true, example = "1"),
            @Parameter(name = "warehouseId", description = "仓库编号", example = "1")
    })
    @PreAuthorize("@ss.hasPermission('erp:product:query')")
    public CommonResult<ErpProductStockWeightRespVO> getProductStockWeight(
            @RequestParam("id") Long productId,
            @RequestParam(value = "warehouseId", required = false) Long warehouseId) {
        
        // 获取商品信息
        ErpProductDO product = productService.getProduct(productId);
        if (product == null) {
            return success(null);
        }
        
        // 构建响应VO
        ErpProductStockWeightRespVO respVO = new ErpProductStockWeightRespVO();
        respVO.setProductId(product.getId());
        respVO.setProductName(product.getName());
        respVO.setBarCode(product.getBarCode());
        respVO.setWeight(product.getWeight());
        
        // 获取总库存数量
        BigDecimal totalStockCount = stockService.getStockCount(productId);
        respVO.setTotalStockCount(totalStockCount);
        
        // 如果指定了仓库ID，获取该仓库的库存数量
        if (warehouseId != null) {
            ErpStockDO stockDO = stockService.getStock(productId, warehouseId);
            if (stockDO != null) {
                respVO.setWarehouseStockCount(stockDO.getCount());
            } else {
                respVO.setWarehouseStockCount(BigDecimal.ZERO);
            }
            respVO.setWarehouseId(warehouseId);
            
            // 获取仓库名称
            ErpWarehouseDO warehouse = warehouseService.getWarehouse(warehouseId);
            if (warehouse != null) {
                respVO.setWarehouseName(warehouse.getName());
            }
        }
        
        return success(respVO);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出产品 Excel")
    @PreAuthorize("@ss.hasPermission('erp:product:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportProductExcel(@Valid ErpProductPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        PageResult<ErpProductRespVO> pageResult = productService.getProductVOPage(pageReqVO);
        // 导出 Excel
        ExcelUtils.write(response, "产品.xls", "数据", ErpProductRespVO.class,
                pageResult.getList());
    }

}